*******************************************************************************
*** Generate the weights matrices for the military spending application.
***
*** Created: 7-24-17
*** Modified: 12-18-18
***
*** NOTES:
*** The difference in size between SLX and SAR weights matrices is because we drop the isolates in the SAR Ws.
*******************************************************************************

clear 
clear matrix
clear mata
set maxvar 30000

*******************************************************************************
****************************** SAR ********************************************
*******************************************************************************

*** Get the correct sample
preserve
	use "Data\Military Spending--Monadic.dta", clear

	sort ccode year
	bys ccode: gen ch2_milex = milex[_n-1] - milex[_n-2]
	
	qui reg ch_milex milex_tm1 ch2_milex log_pop_tm1 civilwar_tm1 total_wars_tm1 alliance_us ch_milex_us ch_milex_ussr
	keep if e(sample)
	gen sample1 = 1 
	
	rename ccode ccode1
	sort ccode1 year
	tempfile cc1
	save `cc1', replace
restore

preserve
	use "Data\Military Spending--Monadic.dta", clear

	sort ccode year
	bys ccode: gen ch2_milex = milex[_n-1] - milex[_n-2]
	
	qui reg ch_milex milex_tm1 ch2_milex log_pop_tm1 civilwar_tm1 total_wars_tm1 alliance_us ch_milex_us ch_milex_ussr
	keep if e(sample)
	gen sample2 = 1 
	
	rename ccode ccode2
	sort ccode2 year
	tempfile cc2
	save `cc2', replace
restore

*** Open up the dyadic dataset
use "Data\Military Spending--Dyadic.dta", clear

sort ccode1 year
merge ccode1 year using `cc1', keep(sample1)
tab _merge
drop if _merge == 2
drop _merge

sort ccode2 year
merge ccode2 year using `cc2', keep(sample2)
tab _merge
drop if _merge == 2
drop _merge

*** Drop all those observations with missing values for either sample
drop if sample1 == . | sample2 == .
drop sample*
sort year ccode1 ccode2

gen id = (year * 1000) + ccode1

****************************** CONTIGUITY *************************************

local wvar cont

*** Create a dataset with isolates so that we can remove them later.
bys ccode1 year: egen numall1 = total(cont)

preserve
	duplicates drop ccode1 year, force
	rename ccode1 ccode
	keep ccode year numall1
	sort year ccode
	saveold "Data\Weights Matrices\SAR\Contiguity\exclude_cont.dta", replace version(12)
restore

levelsof year, local(Y)
qui foreach y of local Y {
	levelsof ccode1 if numall1 == 0 & year == `y', local(C)
	qui foreach c of local C {
		drop if (ccode1 == `c' | ccode2 == `c') & year == `y'
	}
}
	
sort year ccode1 ccode2
keep id ccode2 year `wvar'

local b = 1
levelsof year if inrange(year, 1951, 2008), local(Y)
qui foreach y of local Y {
	preserve
		keep if year == `y'
		reshape wide `wvar', i(id) j(ccode2)
		
		foreach v of varlist `wvar'* {
			rename `v' `v'_`y'
		}
		
		if mod(`b',1) == 0 {
			nois display "." _c
			if mod(`b',20) == 0 {
				nois display ""
			}
		}
		
		tempfile W_`y'
		save `W_`y'', replace
		
		local b = `b' + 1
	restore
}

qui foreach y of local Y {
	local first: word 1 of `Y' 
	if `y' == `first' {
		use `W_`y'', clear
	}
	else {
		append using `W_`y''
	}
}

drop year id
qui foreach v of varlist _all {
	recode `v' (.=0)
}
compress
saveold "Data\Weights Matrices\SAR\Contiguity\W.dta", replace version(12)


*******************************************************************************
****************************** SLX ********************************************
*******************************************************************************

*** Get the correct sample
preserve
	use "Data\Military Spending--Monadic.dta", clear

	qui reg milex milex_tm1 log_pop_tm1 civilwar_tm1 total_wars_tm1 alliance_us milex_us
	keep if e(sample)
	gen sample1 = 1 
	
	rename ccode ccode1
	sort ccode1 year
	tempfile cc1
	save `cc1', replace
restore

preserve
	use "Data\Military Spending--Monadic.dta", clear

	qui reg milex milex_tm1 log_pop_tm1 civilwar_tm1 total_wars_tm1 alliance_us milex_us
	keep if e(sample)
	gen sample2 = 1 
	
	rename ccode ccode2
	sort ccode2 year
	tempfile cc2
	save `cc2', replace
restore

*** Open up the dyadic dataset
use "Data\Military Spending--Dyadic.dta", clear

sort ccode1 year
merge ccode1 year using `cc1', keep(sample1)
tab _merge
drop if _merge == 2
drop _merge

sort ccode2 year
merge ccode2 year using `cc2', keep(sample2)
tab _merge
drop if _merge == 2
drop _merge

*** Drop all those observations with missing values for either sample
drop if sample1 == . | sample2 == .
drop sample*
sort year ccode1 ccode2

gen id = (year * 1000) + ccode1

tempfile data
save `data', replace

******************************** Alliances ************************************

use `data', clear

local wvar alliance

keep id ccode2 year `wvar'

local b = 1
levelsof year if inrange(year, 1951, 2008), local(Y)
qui foreach y of local Y {
	preserve
		keep if year == `y'
		reshape wide `wvar', i(id) j(ccode2)
		
		foreach v of varlist `wvar'* {
			rename `v' `v'_`y'
		}
		
		if mod(`b',1) == 0 {
			nois display "." _c
			if mod(`b',20) == 0 {
				nois display ""
			}
		}
		
		tempfile W_`y'
		save `W_`y'', replace
		
		local b = `b' + 1
	restore
}

qui foreach y of local Y {
	local first: word 1 of `Y' 
	if `y' == `first' {
		use `W_`y'', clear
	}
	else {
		append using `W_`y''
	}
}

drop year id
qui foreach v of varlist _all {
	recode `v' (.=0)
}

compress
saveold "Data\Weights Matrices\SLX\Alliance\W.dta", replace version(12)

**************************** Contiguity ***************************************

use `data', clear

local wvar cont

keep id ccode2 year `wvar'

local b = 1
levelsof year if inrange(year, 1951, 2008), local(Y)
qui foreach y of local Y {
	preserve
		keep if year == `y'
		reshape wide `wvar', i(id) j(ccode2)
		
		foreach v of varlist `wvar'* {
			rename `v' `v'_`y'
		}
		
		if mod(`b',1) == 0 {
			nois display "." _c
			if mod(`b',20) == 0 {
				nois display ""
			}
		}
		
		tempfile W_`y'
		save `W_`y'', replace
		
		local b = `b' + 1
	restore
}

qui foreach y of local Y {
	local first: word 1 of `Y' 
	if `y' == `first' {
		use `W_`y'', clear
	}
	else {
		append using `W_`y''
	}
}

drop year id
qui foreach v of varlist _all {
	recode `v' (.=0)
}

compress
saveold "Data\Weights Matrices\SLX\Contiguity\W.dta", replace version(12)

***************************** Defense Pact ************************************

use `data', clear

local wvar defense

keep id ccode2 year `wvar'

local b = 1
levelsof year if inrange(year, 1951, 2008), local(Y)
qui foreach y of local Y {
	preserve
		keep if year == `y'
		reshape wide `wvar', i(id) j(ccode2)
		
		foreach v of varlist `wvar'* {
			rename `v' `v'_`y'
		}
		
		if mod(`b',1) == 0 {
			nois display "." _c
			if mod(`b',20) == 0 {
				nois display ""
			}
		}

		tempfile W_`y'
		save `W_`y'', replace
		
		local b = `b' + 1
	restore
}

qui foreach y of local Y {
	local first: word 1 of `Y' 
	if `y' == `first' {
		use `W_`y'', clear
	}
	else {
		append using `W_`y''
	}
}

drop year id
qui foreach v of varlist _all {
	recode `v' (.=0)
}

compress
saveold "Data\Weights Matrices\SLX\Defense\W.dta", replace version(12)

***************************** Region ******************************************

use `data', clear

local wvar region

keep id ccode2 year `wvar'

local b = 1
levelsof year if inrange(year, 1951, 2008), local(Y)
qui foreach y of local Y {
	preserve
		keep if year == `y'
		reshape wide `wvar', i(id) j(ccode2)
		
		foreach v of varlist `wvar'* {
			rename `v' `v'_`y'
		}
		
		if mod(`b',1) == 0 {
			nois display "." _c
			if mod(`b',20) == 0 {
				nois display ""
			}
		}
	
		tempfile W_`y'
		save `W_`y'', replace
		
		local b = `b' + 1
	restore
}

qui foreach y of local Y {
	local first: word 1 of `Y' 
	if `y' == `first' {
		use `W_`y'', clear
	}
	else {
		append using `W_`y''
	}
}

drop year id
qui foreach v of varlist _all {
	recode `v' (.=0)
}

compress
saveold "Data\Weights Matrices\SLX\Region\W.dta", replace version(12)


